package com.example.compose.retrofit.crash

import com.example.compose.utils.DataStoreManager
import com.example.compose.utils.FileUtils
import com.example.compose.utils.LogUtil
import com.google.gson.Gson
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File

class UploadLocalLogs {

    fun readLocalLogcat(){

        @OptIn(DelicateCoroutinesApi::class)
        GlobalScope.launch {
            DataStoreManager.logcatFile.collect { value->
                val file = File(value)
                file.list()?.forEach {
                    val logAddress = File(file, it)
                    val readStringToFile = FileUtils.readStringToFile(logAddress)
                    logAddress.delete()
                    readStringToFile?.let { logcat ->
                        LogUtil.i("重新上传日志 $logcat")
                        AddAppArashToRemotely().uploadCrashInfo(Gson().fromJson(logcat,Crash::class.java))
                    }
                }
            }
        }
    }
}